//ConnectActivity.java：连接页面
package com.example.uhf;


import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.apkfuns.logutils.file.LogFileEngine;
import com.apkfuns.logutils.file.LogFileParam;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

import cn.com.example.rfid.driver.Driver;
import cn.com.example.rfid.driver.RfidDriver;


public class MainActivity extends Activity {

	Driver driver;
	String path = "/mnt/sdcard/";
	/*控件变量*/
	private RadioButton rBCom, rBIp;
    private EditText EtCom, EtIp;
    private Button bConnect, bCancel;
	SimpleDateFormat sdfhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private int RbFlag = 0;
	private SoundPool soundPool;
	private int soundId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_connect);

		driver = new RfidDriver();

		InitLog();
		InitView();

		soundPool = new SoundPool(10, AudioManager.STREAM_ALARM, 0);
		soundId = soundPool.load(this, R.raw.beep, 1);
	}


	private void InitLog(){
		LogUtils.getLogConfig()
				.configAllowLog(true)
				.configTagPrefix("LogUtils")
				.configShowBorders(true);
		LogUtils.getLog2FileConfig()
				.configLog2FileEnable(true)
				.configLog2FilePath(path)
				.configLog2FileNameFormat("%d{yyyyMMdd}.txt")
				.configLogFileEngine(new LogFileEngine() {
					@Override
					public void writeToFile(File file, String s, LogFileParam logFileParam) {
						FileOutputStream Outputs;

						if(!file.exists()) {
							try {
								file.createNewFile();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}

						try {
							Outputs = new FileOutputStream(file, true);
							if(Outputs != null){
								try {
									Outputs.write(("----------------------------------------------------------------------------------------" + "\r\n").getBytes());
									Outputs.write((sdfhms.format(System.currentTimeMillis()) +"        " + s + "\r\n").getBytes());
								} catch (IOException e) {
									e.printStackTrace();
								}
							}
						}catch (IOException e){
							e.printStackTrace();
						}
					}
				});
	}


	private void InitView() {
		rBCom 		= findViewById(R.id.rB_Com);
		rBIp 		= findViewById(R.id.rB_Ip);
		EtCom		= findViewById(R.id.Et_Com);
		EtIp		= findViewById(R.id.Et_Ip);
		bConnect	= findViewById(R.id.Bt_Connect);
		bCancel		= findViewById(R.id.Bt_Cancle);

		EtCom.setText("/dev/ttyS1");
		EtIp.setText("192.168.1.10");

		rBCom.setOnClickListener(setRbComIpOnClickListener);
		rBIp.setOnClickListener(setRbComIpOnClickListener);

		bConnect.setOnClickListener(setConnectOnClickListener);
		bCancel.setOnClickListener(setConnectOnClickListener);

	}

	private void DealWith_Rb_Com(){
		if (rBCom.isChecked()){
			RbFlag = 0;
			rBIp.setChecked(false);
			EtIp.setEnabled(false);
			EtCom.setEnabled(true);
		}
	}

	private void DealWith_Rb_Ip(){
		if (rBIp.isChecked()){
			RbFlag = 1;
			rBCom.setChecked(false);
			EtCom.setEnabled(false);
			EtIp.setEnabled(true);
		}
	}

	private OnClickListener setRbComIpOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			switch (view.getId()){
				case R.id.rB_Com:
					DealWith_Rb_Com();
					break;
				case R.id.rB_Ip:
					DealWith_Rb_Ip();
					break;
				default:
					break;
			}
		}
	};

	private void ConnectReader() {
		int Status;

		if (0 == RbFlag){
			Status = driver.initRFID(EtCom.getText().toString(), 115200);
			if(-1000 == Status){
				return;
			}
			Log.i("Connect", "Com:"+EtCom.getText().toString());
		} else  if (1 == RbFlag){
			driver.Net_Init(EtIp.getText().toString(), 20108);
			Log.i("Connect", "IPaddress:"+EtIp.getText().toString());
		}

		//String Fw_buffer;
		//Fw_buffer = driver.readUM7fwOnce();

		//if(Fw_buffer.equals("-1000") || Fw_buffer.equals("-1020")){
		//	Toast.makeText(MainActivity.this, "设备连接失败", Toast.LENGTH_SHORT).show();
		//	return;
		//}else{

			soundPool.play(soundId, 1, 1, 0, 0, 0.5f);

			Intent intent = new Intent(MainActivity.this, ConnectActivity.class);
			startActivity(intent);
		//}

	}

	private void DisconnectReader(){
		int Status;

		if (0 == RbFlag){
			Status = driver.Close_Com();
			if(-1000 == Status){
				return;
			}
			Log.i("DisConnect com", "Com:"+EtCom.getText().toString());
		} else  if (1 == RbFlag){
			int ret = driver.Close_Net();
			Log.i("DisConnect net", "IPaddress:"+EtIp.getText().toString());
		}
	}

	private OnClickListener setConnectOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.Bt_Connect:
				ConnectReader();
				break;
			case R.id.Bt_Cancle:
				DisconnectReader();
				android.os.Process.killProcess(android.os.Process.myPid()); // 杀死该进程
				System.exit(0); // 退出
				finish();
				break;
			default:
				break;
			}
		};
	};


	@Override
	protected void onDestroy() {

		super.onDestroy();
	}


}
